from django.shortcuts import render
from PIL import Image
from mainsite.models import Prodfile
import os
from predict import picture
# Create your views here.

def cropImage(imgfile):
    # 随机生成新的图片名，自定义路径
    ext = imgfile.name.split('.')[-1]
    global file_name
    file_name = '{}.{}'.format('test_complete', ext)
    print(file_name)
    file_pathname = os.path.join("uploadfiles", file_name)
    # 获取图片操作对象
    img = Image.open(imgfile)
    # 相对根目录路径
    filePath = os.path.join("static", "uploadfiles", file_name)
    img.save(filePath)  # 将图片按filePath标识的路径保存在服务器上

    return file_pathname


def index(request):
    if request.POST:
        img = request.FILES.get('avatar')

        if img != None:
            Prodfile.objects.create()
            cropImage(img)
            r_log1,r_log2,r_log3,r_log4 = picture()
            a = 1
            pic_path = "/static/uploadfiles/pre_complete.jpg"

    return render(request, 'index.html',locals())





